$(document).ready(function(){


   //注销
   $(".menulist .list_out").click(function(){
    
    localStorage.setItem("sta",1);
    location.href="./Login.html";
});

$.ajax({
    type:"POST",
    url:"./GetCityServlet",
    data:"",
    async:false,
    success:function(json){
      var msg=JSON.parse(json);
      $(".searchlist .fromcity").empty();
      $(".searchlist .tocity").empty();
       if(msg!=null&&msg.infoList!=null)
       {
           var html;
           for(var i=0;i<msg.infoList.length;i++)
           {
               html+="<option value='"+msg.infoList[i].cityName+"'>"+msg.infoList[i].cityName+"</option>";
           }
           $(".searchlist .fromcity").append(html);
           $(".searchlist .tocity").append(html);
       }
    }
});
if($(".optionone input").get(0).checked)
{
  $(".searchlist").children().eq(4).hide();
}
else
{ $(".searchlist").children().eq(4).show();

}
$(".optionone input").change(function(){
    if($(".optionone input").get(0).checked)
    {
      $(".searchlist").children().eq(4).hide();
    }
    else
    { $(".searchlist").children().eq(4).show();

    }
});
$(".searchlist .fromcity").val(localStorage.getItem("from"));

$(".searchlist .tocity").val(localStorage.getItem("to"));
$(".searchlist .todate").attr("min",$(".searchlist .fromdate").val());

$(".searchlist .fromdate").change(function(){
    $(".searchlist .todate").val($(".searchlist .fromdate").val());
    $(".searchlist .todate").attr("min",$(".searchlist .fromdate").val());
    
});
    $(".searchbutton").click(function(){
     var fromcity=$(".searchlist .fromcity").val();
     var tocity=$(".searchlist .tocity").val();
     var fromdate=$(".searchlist .fromdate").val();
     var todate=$(".searchlist .todate").val();
     var cabin=$(".searchlist .cabin").val();
     $(".msglist").html("");
     $(".msglist2").html("");
     localStorage.setItem("from",fromcity);
     localStorage.setItem("to",tocity);
     searchlist(fromcity,tocity,fromdate,cabin,$(".msglist"));

    
         if($(".optionone input").get(1).checked)
         {
            searchlist(tocity,fromcity,todate,cabin,$(".msglist2"));
         }
   
    });



});

function  searchlist(fromcity,tocity,date,cabin,obj)
{

    $.ajax({
        type:"POST",
        url:"./SearchFlight",
        async:false,
        data:"fromcity="+fromcity+"&tocity="+tocity+"&date="+date,
        success:function(json){
            var msg=JSON.parse(json);
            if(msg!=null&&msg.infoList!=null)
            {
                for(var i=0;i<msg.infoList.length;i++)
                {

                    var price;
                    if(cabin=="Economy")
                    {
                        price=msg.infoList[i].price;
                    }
                    if(cabin=="Business")
                    {
                        price=Math.floor(msg.infoList[i].price)*1.25;
                    }
                    if(cabin=="First")
                    {
                        price=Math.floor(msg.infoList[i].price)*1.5;
                    }

                    var total=Math.floor(msg.infoList[i].total/60)+"h"+msg.infoList[i].total%60+"m";
                    
                  var html=`<div class="innermsg">
                  <div class="optionone" style="margin: auto;">
                      <input name="Flight" type="radio" />Select
                  </div>
                  <div class="innerlist">
                      <p class="">$${price}</p>
                      <p class="">${cabin}</p>
                      <p class="">Flight ${msg.infoList[i].flight}(95%)</p>
                  </div>
                  <div class="innerlist" style="width: 450px;">
                      <div class="placelist"> 
                          <p class="citymsg">${msg.infoList[i].depart}</p>
                          <p class="datemsg">${msg.infoList[i].fromdate}</p>
                      </div>
                      <div class="placelist"> 
                          <div class="citymsg">${msg.infoList[i].arrive}</div>
                          <div class="datemsg">${msg.infoList[i].todate}</div> 
                      </div>
                  </div>
                  <div class="innerlist">
                      <p  class="">Non-stop </p>
                      <p  class="">Total time:${total}</p>
                      <p  class="" style="color: red;">3 available tickets</p>   
                  </div>
              </div>`;
              obj.append(html);
                }
            }
        }
    });


    $.ajax({
        type:"POST",
        url:"./SearchFlight2",
        async:false,
        data:"fromcity="+fromcity+"&tocity="+tocity+"&date="+date,
        success:function(json){
            var msg=JSON.parse(json);
            if(msg!=null&&msg.infoList!=null)
            {
                for(var i=0;i<msg.infoList.length;i++)
                {

                    var price;
                    if(cabin=="Economy")
                    {
                        price=msg.infoList[i].price;
                    }
                    if(cabin=="Business")
                    {
                        price=msg.infoList[i].price*1.25;
                    }
                    if(cabin=="First")
                    {
                        price=msg.infoList[i].price*1.5;
                    }

                    //var total=Math.floor(msg.infoList[i].flight/60)+"h"+msg.infoList[i].flight%60+"m";
                    
                  var html=`<div class="stopinnermsg" >
                  <div class="optionone" style="margin: auto;">
                      <input name="Flight" type="radio" />Select
                  </div>
                  <div class="innerlist" style="height: 120px;">
                      <p  class="">$${price}</p>
                      <p  class="">${cabin}</p>
                      <p  class="">Flight ${msg.infoList[i].flight}(90%)</p>
                      <p  class="">Flight ${msg.infoList[i].flight1}(92%)</p>     
                  </div>
                  <div class="linelist" style="height: 204px;">
                      <div class="placelist">                     
                          <p class="citymsg">${msg.infoList[i].depart}</p>
                          <p class="datemsg">${msg.infoList[i].fromdate}</p>
                          <p class="citymsg">${msg.infoList[i].arrive}</p>
                          <p class="datemsg">${msg.infoList[i].todate}</p>
                      </div>
                      <div class="stoplist"> 
                          <p>2h30m transfer in ${msg.infoList[i].arrive}</p>
                      </div>
                      <div class="placelist">                     
                          <p class="citymsg">${msg.infoList[i].depart1}</p>
                          <p class="datemsg">${msg.infoList[i].fromdate1}</p>
                          <p class="citymsg">${msg.infoList[i].arrive1}</p>
                          <p class="datemsg">${msg.infoList[i].todate1}</p>                
                      </div>
                  </div>
                  <div class="innerlist" >
                      <p>1-stop </p>
                      <p>Total time:16h 50m</p>
                      <p>18 available tickets</p>  
              
                  </div>
              </div>`;
              obj.append(html);
                }
            }
        }
    });
}